package com.lhuc.service.iml;

import com.lhuc.constant.Constants;
import com.lhuc.service.GrantTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.internal.Function;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lhucstart
 * @create 2022-01-21 22:48
 */
@Service
public class QueryGrantTypeService {


    @Autowired
    private GrantTypeService grantTypeService;


    private final Map<String, Function<String, String>> grantTypeMap = new HashMap<>();

    /**
     * 初始化业务分派逻辑,代替了if-else部分
     * key: 优惠券类型
     * value: lambda表达式,最终会获得该优惠券的发放方式
     */
    @PostConstruct
    public void dispatcherInit() {
        grantTypeMap.put(Constants.RED_ENVELOPE, resourceId -> grantTypeService.redPaper(resourceId));
        grantTypeMap.put(Constants.VOUCHERS, resourceId -> grantTypeService.shopping(resourceId));
        grantTypeMap.put(Constants.QQ_MEMBER, resourceId -> grantTypeService.QQVip(resourceId));
    }


    public String getResult(String resourceType) {
        //Controller根据 优惠券类型resourceType、编码resourceId 去查询 发放方式grantType
        Function<String, String> result = grantTypeMap.get(resourceType);
        if (!StringUtils.isEmpty(result)) {
            return result.apply(resourceType);
        }
        return Constants.UN_KNOW;
    }


}
